module.exports = app => {
    const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;

    const Exam = app.model.define('exam', {
      id: {
        type: INTEGER(20),
        primaryKey: true,
        autoIncrement: true,
      },
      name: {
        type: STRING(30),
        allowNull: false,
        defaultValue: "",
        comment: "考试名称",
      },
      class_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "年级班级id"
      },
      status: {
        type: INTEGER(1),
        allowNull: false,
        defaultValue: 0,
        comment: "状态 0取消 1正常",
      },
        created_time: {
          type:DATE,
          get(){
             return app.formatTime(this.getDataValue('created_time'))
          }
        },
        updated_time: DATE,
      });

    // 关联关系
    Exam.associate = function (models) {
       // 与Classes存在多对一关系，所以使用belongsTo()
       app.model.Exam.belongsTo(app.model.Class, {foreignKey: 'class_id', targetKey: 'id'});
        // 关联成绩
        app.model.Exam.hasMany(app.model.Score, { foreignKey: 'exam_id' });
    }

    return Exam;
};